<?php

namespace SinoPHP\BbdBoss\Controller;

use Guzzle\Service\Resource\Model;
use SinoPHP\BbdBoss\AdminController;
use SinoPHP\BbdBoss\Defines\StaticConfig;
use SinoPHP\BbdBoss\Library\Dnspod;

/*
 * Flow tool
 */

class Flow extends AdminController {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 面板
     * 
     * @return
     */
    public function getFlow() {

        $this->view([], 1, 'Flow/getFlow');
    }

    /**
     * 获取日志
     * 
     * @return
     */
    public function getDnspodLog() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/getDnspodLog');
            die;
        }

        $formData = request()->post();
        $environment =  $formData['environment'];
        $domain_type = $formData['domain_type'];
        $project = $formData['project'];
        if ('fuse' == $project) {
            $domain_type = [
                'project' => $project,
                'label' => $domain_type
            ];
        }
        $logNum = $formData['log_num'] ? : 20;
        $dnspodObj = new Dnspod($domain_type, $environment);
        $logList = $dnspodObj->getRecordLogList(['length' => $logNum]);

        echo '<pre>';
        print_r($logList['log']);
        echo '</pre>';
        die; 
    }

    /**
     * 获取记录信息
     * 
     * @return
     */
    public function getRecordInfo() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/getRecordInfo');
            die;
        }

        $formData = request()->post();
        $environment =  $formData['environment'];
        $domainType = $formData['domain_type'];
        $dnspodId = $formData['dnspod_id'];
        $project = $formData['project'];
        if ('fuse' == $project) {
            $domainType = [
                'project' => $project,
                'label' => $domainType
            ];
        }

        $dnspodObj = new Dnspod($domainType, $environment);
        $recordInfo = $dnspodObj->getRecord(['recordId' => $dnspodId]);

        echo '<pre>';
        print_r($recordInfo);
        echo '</pre>';
        die; 
    }

    /**
     * 获取监控信息
     * 
     * @return
     */
    public function getMonitorInfo() {
        if (!request()->isPost()) {
            $this->view([], 1, 'DnspodTool/getMonitorInfo');
            die;
        }

        $formData = request()->post();
        $environment =  $formData['environment'];
        $domainType = $formData['domain_type'];
        $monitorId = $formData['monitor_id'];
        $project = $formData['project'];
        if ('fuse' == $project) {
            $domainType = [
                'project' => $project,
                'label' => $domainType
            ];
        }

        $dnspodObj = new Dnspod($domainType, $environment);
        $monitorInfo = $dnspodObj->getMonitor(['monitorId' => $monitorId]);

        echo '<pre>';
        print_r($monitorInfo);
        echo '</pre>';
        die; 
    }

    public function logout() {
        $loginToken = getAdminUserLoginToken();

        // 记录退出登录日志
        $userLoginInfo = session($loginToken); // 从SESSION获取用户登录信息
        // $userLoginInfo && $this->addActionLog($userLoginInfo['email'] . '退出登录');
        session($loginToken, null); // 删除登录相关信息
        redirect('/AdminUser/login');

        // if (request()->isPost()) {
        //     $this->success([], '退出成功');
        // } else {
        //     redirect('/');
        // }
    }

}
